﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UdrinkWater_Project.Admin
{
    public partial class GoodsPriceChange : System.Web.UI.Page
    {
        string Connection = ConfigurationManager.ConnectionStrings["DB_Udrink"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable Ratetub = GetDataTub();
                txt_Ratetub.Text = Ratetub.Rows[0]["Rate_TubNum"].ToString();

                DataTable RatePet = GetDataPet();
                txt_RatePet.Text = RatePet.Rows[0]["Rate_PackNum"].ToString();

                DataTable RateCrate = GetDataCrate();
                txt_Crate.Text = RateCrate.Rows[0]["Rate_CrateNum"].ToString();

                DataTable RateOneWay = GetDataOneWay();
                txt_OneWay.Text = RateOneWay.Rows[0]["Rate_OneWay_Num"].ToString();
            }
        }
        public DataTable GetDataTub()
        {
            DataTable dt = new DataTable();
            string sqltext = @"SELECT TOP 1 [Rate_TubDate],[Rate_TubNum] FROM [TB_RateTub] ORDER BY [Rate_TubDate] DESC";
            SqlConnection sqlconn = new SqlConnection(Connection);
            SqlDataAdapter da = new SqlDataAdapter(sqltext, sqlconn);
            DataSet ds = new DataSet();
            da.Fill(ds);

            return ds.Tables[0];
        }
        public DataTable GetDataPet()
        {
            DataTable dt = new DataTable();
            string sqltext = @"SELECT TOP 1 [Rate_PackDate],[Rate_PackNum] FROM [TB_Rate_Pack] ORDER BY [Rate_PackDate] DESC";
            SqlConnection sqlconn = new SqlConnection(Connection);
            SqlDataAdapter da = new SqlDataAdapter(sqltext, sqlconn);
            DataSet ds = new DataSet();
            da.Fill(ds);

            return ds.Tables[0];
        }
        public DataTable GetDataCrate()
        {
            DataTable dt = new DataTable();
            string sqltext = @"SELECT TOP 1 [Rate_CrateDate],[Rate_CrateNum] FROM [TB_RateCrate] ORDER BY [Rate_CrateDate] DESC";
            SqlConnection sqlconn = new SqlConnection(Connection);
            SqlDataAdapter da = new SqlDataAdapter(sqltext, sqlconn);
            DataSet ds = new DataSet();
            da.Fill(ds);

            return ds.Tables[0];
        }
        public DataTable GetDataOneWay()
        {
            DataTable dt = new DataTable();
            string sqltext = @"SELECT TOP 1 [Rate_OneWay_Date],[Rate_OneWay_Num] FROM [TB_RateOneWay] ORDER BY [Rate_OneWay_Date] DESC";
            SqlConnection sqlconn = new SqlConnection(Connection);
            SqlDataAdapter da = new SqlDataAdapter(sqltext, sqlconn);
            DataSet ds = new DataSet();
            da.Fill(ds);

            return ds.Tables[0];
        }
        public static object GetDataValue(object value)
        {
            if (value == null)
            {
                return DBNull.Value;
            }
            return value;
        }
        public void InsertsRateTub()
        {
            string sqltext = @"insert into [TB_RateTub] 
                                   ( [Rate_TubDate]
                                    ,[Rate_TubNum]
                                    ,[Act_Admin])
                                   values (@Date,@Num,@Admin)";
            SqlCommand sqlcomm = new SqlCommand();
            SqlConnection sqlconn = new SqlConnection(Connection);
            sqlcomm.Connection = sqlconn;
            sqlcomm.CommandType = System.Data.CommandType.Text;
            sqlcomm.CommandText = sqltext;
            sqlcomm.Parameters.AddWithValue("@Date", GetDataValue(DateTime.Now));
            sqlcomm.Parameters.AddWithValue("@Num", GetDataValue(txt_Ratetub.Text));
            sqlcomm.Parameters.AddWithValue("@Admin", GetDataValue("Admin"));
           
            try
            {
                sqlcomm.Connection.Open();
                sqlcomm.ExecuteNonQuery();
            }
            catch (Exception)
            {
                sqlcomm.Connection.Close();
                sqlcomm.Connection.Dispose();
            }
            sqlcomm.Connection.Close();
            sqlcomm.Connection.Dispose();

        }
        public void InsertsRatePet()
        {
            string sqltext = @"insert into [TB_Rate_Pack] 
                                   ( [Rate_PackDate]
                                    ,[Rate_PackNum]
                                    ,[Act_Admin])
                                   values (@Date,@Num,@Admin)";
            SqlCommand sqlcomm = new SqlCommand();
            SqlConnection sqlconn = new SqlConnection(Connection);
            sqlcomm.Connection = sqlconn;
            sqlcomm.CommandType = System.Data.CommandType.Text;
            sqlcomm.CommandText = sqltext;
            sqlcomm.Parameters.AddWithValue("@Date", GetDataValue(DateTime.Now));
            sqlcomm.Parameters.AddWithValue("@Num", GetDataValue(txt_RatePet.Text));
            sqlcomm.Parameters.AddWithValue("@Admin", GetDataValue("Admin"));

            try
            {
                sqlcomm.Connection.Open();
                sqlcomm.ExecuteNonQuery();
            }
            catch (Exception)
            {
                sqlcomm.Connection.Close();
                sqlcomm.Connection.Dispose();
            }
            sqlcomm.Connection.Close();
            sqlcomm.Connection.Dispose();

        }
        public void InsertsRateCrate()
        {
            string sqltext = @"insert into [TB_RateCrate] 
                                   ( [Rate_CrateDate]
                                    ,[Rate_CrateNum]
                                    ,[Act_Admin])
                                   values (@Date,@Num,@Admin)";
            SqlCommand sqlcomm = new SqlCommand();
            SqlConnection sqlconn = new SqlConnection(Connection);
            sqlcomm.Connection = sqlconn;
            sqlcomm.CommandType = System.Data.CommandType.Text;
            sqlcomm.CommandText = sqltext;
            sqlcomm.Parameters.AddWithValue("@Date", GetDataValue(DateTime.Now));
            sqlcomm.Parameters.AddWithValue("@Num", GetDataValue(txt_Crate.Text));
            sqlcomm.Parameters.AddWithValue("@Admin", GetDataValue("Admin"));

            try
            {
                sqlcomm.Connection.Open();
                sqlcomm.ExecuteNonQuery();
            }
            catch (Exception)
            {
                sqlcomm.Connection.Close();
                sqlcomm.Connection.Dispose();
            }
            sqlcomm.Connection.Close();
            sqlcomm.Connection.Dispose();

        }
        public void InsertsRateOneWay()
        {
            string sqltext = @"insert into [TB_RateOneWay] 
                                   ( [Rate_OneWay_Date]
                                    ,[Rate_OneWay_Num]
                                    ,[Act_Admin])
                                   values (@Date,@Num,@Admin)";
            SqlCommand sqlcomm = new SqlCommand();
            SqlConnection sqlconn = new SqlConnection(Connection);
            sqlcomm.Connection = sqlconn;
            sqlcomm.CommandType = System.Data.CommandType.Text;
            sqlcomm.CommandText = sqltext;
            sqlcomm.Parameters.AddWithValue("@Date", GetDataValue(DateTime.Now));
            sqlcomm.Parameters.AddWithValue("@Num", GetDataValue(txt_OneWay.Text));
            sqlcomm.Parameters.AddWithValue("@Admin", GetDataValue("Admin"));

            try
            {
                sqlcomm.Connection.Open();
                sqlcomm.ExecuteNonQuery();
            }
            catch (Exception)
            {
                sqlcomm.Connection.Close();
                sqlcomm.Connection.Dispose();
            }
            sqlcomm.Connection.Close();
            sqlcomm.Connection.Dispose();

        }
        protected void bt_EditTub_Click(object sender, EventArgs e)
        {
            txt_Ratetub.ReadOnly = false;
            bt_SaveTub.Enabled = true;
            bt_EditTub.Enabled = false;
            txt_Ratetub.Focus();
        }

        protected void bt_SaveTub_Click(object sender, EventArgs e)
        {
            if (txt_Ratetub.Text.Length > 0)
            {
                InsertsRateTub();
                GV_RateTub.DataBind();
            }
            DataTable Ratetub = GetDataTub();
            txt_Ratetub.Text = Ratetub.Rows[0]["Rate_TubNum"].ToString();
            txt_Ratetub.ReadOnly = true;
            bt_SaveTub.Enabled = false;
            bt_EditTub.Enabled = true;
        }

        protected void bt_EditPet_Click(object sender, EventArgs e)
        {
            txt_RatePet.ReadOnly = false;
            bt_SavePet.Enabled = true;
            bt_EditPet.Enabled = false;
            txt_RatePet.Focus();
        }

        protected void bt_SavePet_Click(object sender, EventArgs e)
        {
            if (txt_RatePet.Text.Length > 0)
            {
                InsertsRatePet();
                GV_RatePet.DataBind();
            }
            DataTable RatePet = GetDataPet();
            txt_RatePet.Text = RatePet.Rows[0]["Rate_PackNum"].ToString();
            txt_RatePet.ReadOnly = true;
            bt_SavePet.Enabled = false;
            bt_EditPet.Enabled = true;
        }

        protected void bt_EditCrate_Click(object sender, EventArgs e)
        {
            txt_Crate.ReadOnly = false;
            bt_SaveCrate.Enabled = true;
            bt_EditCrate.Enabled = false;
            txt_Crate.Focus();
        }

        protected void bt_SaveCrate_Click(object sender, EventArgs e)
        {
            if (txt_Crate.Text.Length > 0)
            {
                InsertsRateCrate();
                GV_RateCrate.DataBind();
            }
            DataTable RateCrate = GetDataCrate();
            txt_Crate.Text = RateCrate.Rows[0]["Rate_CrateNum"].ToString();
            txt_Crate.ReadOnly = true;
            bt_SaveCrate.Enabled = false;
            bt_EditCrate.Enabled = true;
        }

        protected void bt_EditOneWay_Click(object sender, EventArgs e)
        {
            txt_OneWay.ReadOnly = false;
            bt_SaveOneWay.Enabled = true;
            bt_EditOneWay.Enabled = false;
            txt_OneWay.Focus();
        }

        protected void bt_SaveOneWay_Click(object sender, EventArgs e)
        {
            if (txt_OneWay.Text.Length > 0)
            {
                InsertsRateOneWay();
                GV_RateOneWay.DataBind();
            }
            DataTable RateOneWay = GetDataOneWay();
            txt_OneWay.Text = RateOneWay.Rows[0]["Rate_OneWay_Num"].ToString();
            txt_OneWay.ReadOnly = true;
            bt_SaveOneWay.Enabled = false;
            bt_EditOneWay.Enabled = true;
        }
    }
}